<?php

error_reporting(E_ALL);


if (0 > version_compare(PHP_VERSION, '5')) {
    die('This file was generated for PHP 5');
}

class Publicite
{

	private $id_publicite = null;
	
	private $position = null;
	
	private $URL = null;
	
	private $date_debut = null;
	
	private $date_fin = null;
	
	private $id_journal = null;
	
	public function getIdPublicite(){
		return $this->id_publicite;
	}
	
	public function setIdPublicite($id_publicite){
		$this->id_publicite = $id_publicite;
	}
	
	public function getPosition(){
		return $this->position;
	}
	
	public function setPosition($position){
		$this->position = $position;
	}
	
	public function getURL(){
		return $this->URL;
	}
	
	public function setURL($URL){
		$this->URL = $URL;
	}
	
	public function getDateDebut(){
		return $this->date_debut;
	}
	
	public function setDateDebut($dateDebut){
		$this->date_debut = $dateDebut;
	}
	
	public function getDateFin(){
		return $this->date_fin;
	}
	
	public function setDateFin($dateFin){
		$this->date_fin = $dateFin;
	}
	
	public function getIdJournal(){
		return $this->id_journal;
	}
	
	public function setIdJournal($IdJournal){
		$this->id_journal = $IdJournal;
	}
	
	public function recupLastPublicite($Position){
	
		// Récupération de la connexion et préparation de la requête.
		$tbh = ConnexionBD::getInstance();
		
		$stat = $tbh->prepare("SELECT * FROM publicite WHERE position = :position and date_debut <= CURDATE() and date_fin >= CURDATE() ORDER BY id_publicite");
		$array = array(	':position' 	=> 	$Position	);
		$stat->execute($array);
		$arrValues = $stat->fetchAll(PDO::FETCH_ASSOC);
		foreach ($arrValues as $row){
			$this->setIdPublicite($row['id_publicite']);
			$this->setPosition($Position);
			$this->setURL($row['URL']);
			$this->setDateDebut($row['date_debut']);
			$this->setDateFin($row['date_fin']);
			$this->setIdJournal($row['id_journal']);
		}
	}
	
	public function recupPublicite($id){
	
		// Récupération de la connexion et préparation de la requête.
		$tbh = ConnexionBD::getInstance();
		
		$stat = $tbh->prepare("SELECT * FROM publicite WHERE id_publicite = :id");
		$array = array(	':id' 	=> 	$id	);
		$stat->execute($array);
		$arrValues = $stat->fetchAll(PDO::FETCH_ASSOC);
		foreach ($arrValues as $row){
			$this->setIdPublicite($row['id_publicite']);
			$this->setPosition($row['position']);
			$this->setURL($row['URL']);
			$this->setDateDebut($row['date_debut']);
			$this->setDateFin($row['date_fin']);
			$this->setIdJournal($row['id_journal']);
		}
	}
	
	public static function getPubJournal($journalId){
		$tbh = ConnexionBD::getInstance();
		
		$stat = $tbh->prepare("SELECT * FROM publicite WHERE id_journal = :id");
		$array = array(	':id' 	=> 	$journalId	);
		$stat->execute($array);
		$arrValues = $stat->fetchAll(PDO::FETCH_ASSOC);
		
		$pubs = array();
		
		foreach ($arrValues as $row){
			$pub = new Publicite();
			$pub->setIdPublicite($row['id_publicite']);
			$pub->setPosition($row['position']);
			$pub->setURL($row['URL']);
			$pub->setDateDebut($row['date_debut']);
			$pub->setDateFin($row['date_fin']);
			$pub->setIdJournal($row['id_journal']);
			$pubs[] = $pub;
		}
		
		return $pubs;
		
	}
	
	public function export(){
		$tbh = ConnexionBD::getInstance();
		
		$stat = $tbh->prepare("INSERT INTO publicite SET URL=:url, id_journal=:id");
		$array = array(	':id' 	=> 	$this->id_journal,
						':url'	=> $this->URL	);
		$stat->execute($array);
	}

} /* end of class Publicite */

?> 